package com.szgr.baoyin.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.ZoomControls;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.szgr.baoyin.MyApplication;
import com.szgr.baoyin.R;
import com.szgr.baoyin.utils.Constant;
import com.szgr.baoyin.utils.RLog;
import com.szgr.baoyin.volley.Volley;
import com.szgr.baoyin.volley.VolleyInterface;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 预约人员路线图
 */
public class OrderMapActivity extends BaseActivity {

    private String id;
    private BaiduMap baiduMap;
    private boolean flag = true;
    private MapView orderMap;
    private boolean firstIn = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_order_map);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initActionBar() {
        actionBarTitle.setText("服务人员位置");
    }

    @Override
    public void initView() {
        orderMap = (MapView) findViewById(R.id.order_map);
        baiduMap = orderMap.getMap();
        baiduMap.setMyLocationEnabled(true);
        orderMap.showScaleControl(false);
        orderMap.showZoomControls(false);
        View logoView = orderMap.getChildAt(1);
        if(logoView != null && (logoView instanceof ImageView || logoView instanceof ZoomControls)){
            logoView.setVisibility(View.INVISIBLE);
        }

        Intent intent = getIntent();
        if(intent != null && intent.hasExtra("id")){
            id = intent.getStringExtra("id");
            new LocationThread().start();
        }
    }

    class LocationThread extends Thread {
        @Override
        public void run() {
            RLog.s("---->location.......................");
            while(flag) {
                Volley.get(Constant.URL_SERVER_LOCATION + "&technician_id=" + id + "&key=" + MyApplication.getmInstance().getKey(), "server location", new VolleyInterface() {
                    @Override
                    public void onSuccess(String s, String tag) {
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            JSONObject datas = jsonObject.getJSONObject("datas");
                            JSONObject technician = datas.getJSONObject("technician");
                            String position = technician.getString("position");
                            String[] split = position.split(",");
                            locationCurrentSite(Double.parseDouble(split[1]), Double.parseDouble(split[0]));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailed(String tag) {

                    }
                });
                try {
                    sleep(5 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.action_bar_left:
                finish();
                break;
        }
    }

    /**
     * 定位当前地位置
     * @param
     */
    private void locationCurrentSite(double lat,double lon) {

        MyLocationData locData = new MyLocationData.Builder()
//                .accuracy(bdLocation.getRadius())
//                .direction(bdLocation.getDirection())
                .latitude(lat)
                .longitude(lon)
                .build();
        baiduMap.setMyLocationData(locData);

        BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.icon_location_now);

        if(firstIn) {
//            OverlayOptions options = new CircleOptions()
//                    .center(currentLatLng)
//                    .radius(20)
//                    .fillColor(0xaac7dee9)
//                    .stroke(new Stroke(3,0xff80a3d0));
////                    baiduMap.clear();
//            baiduMap.addOverlay(options);

            baiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(new MapStatus.Builder().zoom(19).build()));
            MyLocationConfiguration config = new MyLocationConfiguration(MyLocationConfiguration.LocationMode.FOLLOWING, true, bitmapDescriptor);
            baiduMap.setMyLocationConfigeration(config);
            firstIn = false;
        }else{

            MyLocationConfiguration config = new MyLocationConfiguration(MyLocationConfiguration.LocationMode.NORMAL, true, bitmapDescriptor);
            baiduMap.setMyLocationConfigeration(config);
//            MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(new LatLng(lat,lon));
//            baiduMap.animateMapStatus(msu);
        }

//        baiduMap.setMyLocationEnabled(false);
    }

    @Override
    protected void onResume() {
        super.onResume();
        orderMap.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        orderMap.onPause();
    }

    @Override
    protected void onDestroy() {
        flag = false;
        super.onDestroy();
        orderMap.onDestroy();
    }
}
